﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class RatingRepository : HitchHikersGuideToTheGalaxy.Models.IRatingRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add rating
        /// </summary>
        /// <param name="rating"></param>
        public void Add(Rating rating)
        {
            db.Ratings.InsertOnSubmit(rating);
        }

        /// <summary>
        /// Delete rating
        /// </summary>
        /// <param name="rating"></param>
        public void Delete(Rating rating)
        {
            db.Ratings.DeleteOnSubmit(rating);
        }

        /// <summary>
        /// Save changes to database
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Find all Ratings
        /// </summary>
        /// <returns></returns>
        public IQueryable<Rating> FindAllRatings()
        {
            return db.Ratings;
            //TODO: fá grade með
        }

        /// <summary>
        /// Find a rating
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Rating FindRating(int id)
        {
            return db.Ratings.SingleOrDefault(d => d.Id == id);
        }

        /// <summary>
        /// Find rating given by me
        /// </summary>
        /// <param name="ssn"></param>
        /// <returns></returns>
        public IQueryable<Rating> FindRatingsGivenByMe(String ssn)
        {
            return from rating in db.Ratings
                   where ssn == rating.UserInfoSSNGive
                   orderby rating.CreatedTime
                   select rating;
            //TODO: fá grade með
        }

        /// <summary>
        /// Find rating given to me
        /// </summary>
        /// <param name="ssn"></param>
        /// <returns></returns>
        public IQueryable<Rating> FindRatingsGivenToMe(String ssn)
        {
            return from rating in db.Ratings
                   where ssn == rating.UserInfoSSNRecieve
                   orderby rating.CreatedTime
                   select rating;
            //TODO: fá grade með
        }

        #endregion
    }
}
